//including standard libraries
#include <stdlib.h>
#include <stdio.h>
//including japi functions
#include "japi.h"


//check number function
int check_file_type(int count_func_var)
{
    //declare variable
    int decider_number;
    //if statement if it's a no file
    if ((count_func_var < 250 && count_func_var > 30)||(count_func_var < 4785 && count_func_var > 4730))
    {
        //giving value to the decider variable
        decider_number = 0;
    }
    //else if statement if it's a yes file
    else if ((count_func_var < 1950 && count_func_var > 450)||(count_func_var < 4660 && count_func_var > 4600))
    {
        //giving value to the decider variable
        decider_number=1;
    }
    //if the sample file is silence
    else if (count_func_var < 10100 && count_func_var > 10000)
    {
        //giving value to the decider variable
        decider_number=2;
    }
    else//if the sample file is not valid
    {
        //giving value to the decider variable
        decider_number=3;
    }
    //returning the value
    return(decider_number);
}

int*load_file_function(char *file_pointer)
{

    FILE* fp;

    int counter=0, i=0,*ptrcounter,ptr2ptr[2];
    float temp, *array_ptr;//declaring pointer


    int j = 0;
    int countHeaderLines = 0;
    char stringCheck[100] = {0};


    fp=fopen(file_pointer,"r");// Some code written into a file to just test values were being read in

    printf("test\n");

    if(fp==NULL)//error checking code
    {
        printf("Error In Opening File\n");
        system("PAUSE");
        exit(-1);
    }

    else//else statement to determine the file opened successfully
    {

        printf("File Opened Successfully\n");

        while((fscanf(fp,"%f",&temp))!=EOF) //incrementing counter variable to determine the number of values
        {

            fgets (stringCheck , 100 , fp);
            if(stringCheck[0] == '#')
            {
               countHeaderLines = countHeaderLines + 1;
            }
            else
            //Set the number of elements in the file
            {
                counter = counter + 1;
            }

        }

        fp=fopen(file_pointer,"r");//reading in the files elements

     for(j = 0; j < countHeaderLines; j++)
     {
          fgets (stringCheck, 100 , fp);
     }

        array_ptr = (float *) calloc(counter, sizeof(float));//allocating the amount of space in memory

        if (array_ptr==NULL)//error checking abou the memory allocation
        {
            printf("Memory Allocation Failed\n\n");

            system("PAUSE");
            return 1;
        }

        //for loop filling memory from the pointer's position with values read from file
        for(i=0;i<counter;i++)
        {
            fscanf(fp,"%f",&array_ptr[i]);
        }

        ptrcounter=counter;

        ptr2ptr[0]=ptrcounter;//putting the two variables into the float array ptr2ptr
        ptr2ptr[1]=array_ptr;

        return(ptr2ptr);//returning the array with two elements
    }

}
//Function to find the crossover's.
int get_zero(int *p1, int *p)
{
     //Define variables.
     int i;
     int cntr=0;

    //For loop to compare the elements in the file.
    for(i=0;i<p;i++)
    {
        //If statment to count the number fo crossover.
        if ((p1[i]<0&&p1[i+1]>0)||(p1[i]>0&&p1[i+1]<0))
        {
           //Add 1 to cntr.
           cntr=cntr+1;
        }
    }

    //Return the value.
    return (cntr);
}
//gui function
void gui(void)
{
    //declaring variables
    int main_frame,obj,display_label,answer_label;
    int file,load,menubar,quit;
    int *text_file_pointer;
    int no_of_crossovers;
    int check_value;
    char text_file[512],*file_check;

    //to analyize the program
    j_setdebug(2);

    //if the japi application doesn't load
    if(j_start()<0)
        printf("can't connect to server\n"),exit(0);

    //creating the window frame and giving it a name
    main_frame = j_frame("Speech Recognition System");
    //creating a menu bar within the frame
    menubar = j_menubar (main_frame);
    //creating the menu drop down from the menu bar
    file = j_menu(menubar,"File");
    //creating the open file option from the file menu
    load = j_menuitem(file,"Open");
    //creating the quit option from the file menu
    quit = j_menuitem(file,"Quit");

    //dividing frame to 2 spaces
    j_setgridlayout(main_frame,2,1);

    //creating the label within the frame
    display_label = j_label(main_frame,"The Existentialists Speech Recognition System");
    answer_label = j_label(main_frame,"");

    //auto sizing the window
    j_pack(main_frame);
    //displaying the frame
    j_show(main_frame);
    //infinite loop
    while(1)
    {
        //for the CPU to keep checking whether anything has been actioned
        obj =  j_nextaction();
        //if the load file option was selected
        if(obj==load)
        {
            //to open a file and save it within a variable
            file_check = j_filedialog(main_frame,"Open File","",text_file);
            //checking file opened
            if(file_check!=text_file)
            {
                printf("Could not open file\n");
                exit (-1);
            }
            //function call to load file
            text_file_pointer=load_file_function(file_check);
            //function call to the crossover count function
            no_of_crossovers = get_zero(text_file_pointer[1],text_file_pointer[0]);
            //function call to the check function
            check_value = check_file_type(no_of_crossovers);
            //if statement if it's a yes file
            if(check_value==1)
            {
                //display message
                j_settext(answer_label,"This is a yes file");
                j_show(answer_label);
            }
            //else statement if it's a no file
            else if(check_value==0)
            {
                //display message
                j_settext(answer_label,"This is a no file");
                j_show(answer_label);
            }
            else
            {
                //display message if the file is not valid
                j_settext(answer_label,"This is an invalid file");
                j_show(answer_label);
            }
        }
        //to exit the window by either the frame or the quit button
        else if((obj==quit) || (obj == main_frame))
            break;

    }
    //quiting the japi application
    j_quit();

    return 0;
}






